package com.wswy.wyjk.ui.practiceNew;

import android.app.Activity;
import android.graphics.Point;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.my.httpapi.api.baseUtils.LogUtils;
import com.qq.e.ads.banner2.UnifiedBannerADListener;
import com.qq.e.ads.banner2.UnifiedBannerView;
import com.qq.e.comm.util.AdError;

import java.util.Locale;

public class UnifiedBannerController {
    private Activity context;
    private ViewGroup bannerContainer;
    private UnifiedBannerView bv;
    private String posId;

    public UnifiedBannerController(Activity context, ViewGroup bannerContainer,String posId,boolean isSHow){
        this.context=context;
        this.bannerContainer=bannerContainer;
        this.posId=posId;
        if(isSHow) {
            getBanner().loadAD();
        }
    }

    public void onDestroy() {
        if (bv != null) {
            bv.destroy();
        }
    }

    public void doCloseBanner() {
        bannerContainer.removeAllViews();
        if (bv != null) {
            bv.destroy();
            bv = null;
        }
    }

    private UnifiedBannerView getBanner() {
        if( this.bv != null && this.posId.equals(posId)) {
            return this.bv;
        }
        if(this.bv != null){
            bannerContainer.removeView(bv);
            bv.destroy();
        }
        this.bv = new UnifiedBannerView(context, posId, unifiedBannerADListener);
        bannerContainer.addView(bv, getUnifiedBannerLayoutParams());
        return this.bv;
    }

    /**
     *  Banner 规定banner宽高比应该为6.4:1 , 开发者可自行设置符合规定宽高比的具体宽度和高度值
     *
     * @return
     */
    private FrameLayout.LayoutParams getUnifiedBannerLayoutParams() {
        Point screenSize = new Point();
        context.getWindowManager().getDefaultDisplay().getSize(screenSize);
        return new FrameLayout.LayoutParams(screenSize.x,  Math.round(screenSize.x / 6.4F));
    }



    private UnifiedBannerADListener unifiedBannerADListener=new UnifiedBannerADListener(){

        @Override
        public void onNoAD(AdError adError) {
            bannerContainer.setVisibility(View.GONE);
        }

        @Override
        public void onADReceive() {
            bannerContainer.setVisibility(View.VISIBLE);
            LogUtils.e("onADReceive");
        }

        @Override
        public void onADExposure() {
            LogUtils.e("onADExposure");
        }

        @Override
        public void onADClosed() {
            bannerContainer.setVisibility(View.GONE);
            LogUtils.e("onADClosed");
        }

        @Override
        public void onADClicked() {
            LogUtils.e("onADClicked");
        }

        @Override
        public void onADLeftApplication() {
            LogUtils.e("onADLeftApplication");
        }

        @Override
        public void onADOpenOverlay() {
            LogUtils.e("onADOpenOverlay");
        }

        @Override
        public void onADCloseOverlay() {
            LogUtils.e("onADCloseOverlay");
        }
    };
}
